/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"github.com/pkg/errors"
	"github.com/rs/zerolog/log"
	"github.com/spf13/viper"
	"kvmc/app"

	"github.com/spf13/cobra"
)

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
	Use:     "delete",
	Aliases: []string{"del", "rm"},
	Short:   "删除node",
	Long:    ``,
	Args: func(cmd *cobra.Command, args []string) error {
		if deleteOpt.Name == "" && len(args) > 0 {
			deleteOpt.Name = args[0]
		}
		if !deleteOpt.All && deleteOpt.Name == "" {
			return errors.New("需要指定node的名称")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		manager, err := app.NewManager(viper.GetString("configPath"))
		if err != nil {
			log.Err(err).Send()
			return
		}
		manager.Delete(deleteOpt)
		err = manager.Save()
		if err != nil {
			log.Err(err).Send()
			return
		}
	},
}

var deleteOpt = new(app.DeleteOption)

func init() {
	rootCmd.AddCommand(deleteCmd)

	deleteCmd.Flags().StringVarP(&deleteOpt.ClusterName, "cluster", "c", "", "node的集群名称")
	deleteCmd.Flags().BoolVarP(&deleteOpt.All, "all", "A", false, "所有node")
	deleteCmd.Flags().StringVarP(&deleteOpt.Name, "name", "n", "", "node的名称")
}
